﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data;
namespace Text1
{
    class AccountHander:IHandler
    {
        public void Register(Dictionary<Protocal, Action<SockedClient, ByteBuffer>> handlers)
        {
            handlers.Add(Protocal.Login_CMD, AccountLogin);
            handlers.Add(Protocal.LogOut_CND, AccountLogOut);
        }
        void AccountLogin(SockedClient client,ByteBuffer buffer)
        {
            ReqLogin log = ProtoHelper.Deserialize<ReqLogin>(buffer.ReadBytes());
            if (ActorManager.Instance.HasActor(log))
            {
                log.errorCode = new ErrorCode
                {
                    code = 0,
                    error = "您的帐号已经登陆"
                };
               GameSever.Instance.SendClient(client, Protocal.Login_CMD, log);
            }
            else
            {
                //1 通知自己进入游戏//2 通知其他玩家
                ReqClient reqclient = new ReqClient();
                Random r = new Random();
                //新玩家进入初始化信息
                Account account = new Account
                {
                    username = log.username,
                    nickname = log.nickname,
                    point = r.Next(0, 4),
                    hp=5,
                    defaultHp=5,
                    color = new Color
                    {
                        r = (float)r.NextDouble(),
                        g = (float)r.NextDouble(),
                        b = (float)r.NextDouble(),
                    }
                };
                reqclient.errorCode = new ErrorCode
                {
                    code = 1,
                    error = "登陆成功"
                };
                reqclient.localActor = account;
                Console.WriteLine(log.username + "登录游戏 在" + reqclient.localActor.point);
                reqclient.actors.AddRange(ActorManager.Instance.GetAllLogins());
               GameSever.Instance.SendClient(client, Protocal.Login_CMD, reqclient);
                //3 通知其他玩家进入自己的场景                                              
                GameSever.Instance.SendOther(client, Protocal.OtherLogin_CMD, account);
                //最后把自己加进actors
                ActorManager.Instance.Add(account, client);
            }
        }
        void AccountLogOut(SockedClient client,ByteBuffer buffer)
        {

        }
    }
}
